package com.fyzk.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.fyzk.interfaces.MoreInterface;
import com.tyjc.lottery.R;
import com.fyzk.utils.L;


public class ListMoreView {
	
	private final String TAG = this.getClass().getSimpleName();
	
	private final int MIN_INDEX = 1;
	private View view;
	private Context context;
	private int newPageIndex = 1;//默认是1
	int maxPageIndex;
	ProgressBar progressbar;
	ListView listView;
	MoreInterface moreOnclick;
	private TextView textAlert;
    public ListMoreView(Context context,ListView listView){
    	this.context = context;
    	this.listView = listView;
    	initView();
    }
    
    public ListMoreView(Context context){
    	this.context = context;
    	LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	view = mInflater.inflate(R.layout.lookmorebtn, null);
    	textAlert = (TextView) view.findViewById(R.id.notice_prize_getmore_text);
    	progressbar = (ProgressBar) view.findViewById(R.id.getmore_progressbar);
    	view.setOnClickListener(new OnClickListener() {

    		@Override
    		public void onClick(View v) {
    			// TODO Auto-generated method stub
    			L.v(TAG, "onClick>>>>>>>>>>>>>>>>");
    			progressbar.setVisibility(ProgressBar.VISIBLE);
    			// TODO Auto-generated method stub
    			moreOnclick.more();
    			view.setEnabled(false);
    		}
    	});
    }
    public void initView(){
    	LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	view = mInflater.inflate(R.layout.lookmorebtn, null);
    	textAlert = (TextView) view.findViewById(R.id.notice_prize_getmore_text);
    	progressbar = (ProgressBar) view.findViewById(R.id.getmore_progressbar);
    	view.setOnClickListener(new OnClickListener() {

    		@Override
    		public void onClick(View v) {
    			// TODO Auto-generated method stub
    			L.v(TAG, "onClick>>>>>>>>>>>>>>>>");
    			progressbar.setVisibility(ProgressBar.VISIBLE);
    			// TODO Auto-generated method stub
    			moreOnclick.more();
    			view.setEnabled(false);
    		}
    	});
    }
    public View getView(){
    	return view;
    }
    public void setViewGone(){
    	view.setVisibility(View.GONE);
    }
    public void setViewVisble(){
    	view.setVisibility(View.VISIBLE);
    }
    public void startNet(){
    	progressbar.setVisibility(ProgressBar.VISIBLE);
    	view.setVisibility(View.VISIBLE);
    }
    public void noNet(){
    	progressbar.setVisibility(ProgressBar.INVISIBLE);
    	view.setVisibility(View.VISIBLE);
    }
    public void setTextStr(String alertStr){
      	progressbar.setVisibility(ProgressBar.INVISIBLE);
      	textAlert.setText(alertStr);
    }
    public void initStartNet(){
    	progressbar.setVisibility(ProgressBar.VISIBLE);
    	textAlert.setText("获取更多");
    	view.setVisibility(View.VISIBLE);
    }
    
    public void setClickable(boolean clickable){
//    	view.setClickable(clickable);
    	view.setEnabled(clickable);
    }
    
    /**
     * 联网结束后事件
     */
    public void endNet(){
    	progressbar.setVisibility(ProgressBar.INVISIBLE);
		view.setEnabled(true);
    }
    public void setMoreOnClick(MoreInterface moreOnclick){
    	this.moreOnclick = moreOnclick;
    }
	public int getNewPageIndex() {
		return newPageIndex;
	}
	public void setNewPageIndex(int newPageIndex) {
		this.newPageIndex = newPageIndex;
	}
	public int getMaxPageIndex() {
		return maxPageIndex;
	}
	public void setMaxPageIndex(int maxPageIndex) {
		this.maxPageIndex = maxPageIndex;
	}
	
	public boolean pageIndexAdd(){
		int newIndex = newPageIndex;
		newIndex++;
		if(newIndex<maxPageIndex){
			newPageIndex = newIndex;
			return true;
		}else{
			Toast.makeText(context, "已经获取全部数据", Toast.LENGTH_SHORT);
			return false;
		}
	}
	public void pageIndexSub(){
		int newIndex = newPageIndex;
		newIndex--;
		if(newIndex>=MIN_INDEX){
			newPageIndex = newIndex;
		}
	}
}
